package cn.yonyong.base.project.config.AOP;

import cn.yonyong.base.project.config.AOP.annotation.Log;
import cn.yonyong.base.project.entity.TblLog;
import cn.yonyong.base.project.mapper.TblLogMapper;
import com.alibaba.druid.support.json.JSONUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

/**
 * @Describtion Todo
 * @Author yonyong
 * @Date 2019/10/30 15:37
 * @Version 1.0.0
 **/
@Aspect
@Component
public class SystemLogAop {
    @Resource
    TblLogMapper tblLogMapper;

    @Pointcut("@annotation(cn.yonyong.base.project.config.AOP.annotation.Log)")
    public void pointcut(){}

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = joinPoint.proceed();
        //执行时常(毫秒)
        long time = System.currentTimeMillis()-beginTime;
        //保存日志
        savaLog(joinPoint,time);
        return result;
    }

    private void savaLog(ProceedingJoinPoint joinPoint, long time) throws UnknownHostException {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        TblLog tblLog = new TblLog();
        Log log = method.getAnnotation(Log.class);
        if (log != null){
            //注解上的描述
            tblLog.setOperation(log.value());
            tblLog.setComment(log.comment());
        }
        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        tblLog.setMethod(className+"."+methodName+"()");
        //请求的参数
        Object[] args = joinPoint.getArgs();
        String paramString = args[0].toString();
        if (null != paramString && paramString.length()>4999){
            paramString = paramString.substring(0,4999);
        }
        String params = JSONUtils.toJSONString(paramString);
        tblLog.setParams(params);

        //获取request
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        //设置ip地址
        InetAddress ip4 = InetAddress.getLocalHost();
        String ip = ip4.getHostAddress();

        //设置用户相关信息    默认setUserid 1  setUsername  yonyong
        tblLog.setUserid(1);
        tblLog.setUsername("yonyong");

        //设置方法执行时间
        tblLog.setTime((int) time);

        //设置系统当前时间
        Date date = new Date();
        tblLog.setGmtcreate(date);

        //保存系统日志
        tblLogMapper.insert(tblLog);
    }
}
